package leetcode.N101_N200;

import java.util.HashSet;
import java.util.Set;

/**
 * @author xiwai [hww282306@alibaba-inc.com]
 * @version 1.0
 * @date 2020/10/20 4:24 下午
 */
public class N128 {

    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>(nums.length);
        for (int num : nums) {
            set.add(num);
        }
        int longVal = 0;
        int currentVal = 0;

        for (int num : nums) {
            if (set.contains(num - 1)) {
                continue;
            }
            while (set.contains(num++)) {
                currentVal++;
            }
            longVal = Math.max(longVal, currentVal);
            currentVal = 0;
        }
        return longVal;
    }

}
